import Vuex from "vuex"
import Vue from "vue"
import createPersistedState from 'vuex-persistedstate'
import axios from 'axios'
import '../../static/constant/api/userApi.js'

Vue.use(Vuex);

const state = {
    user: null
};
const mutations = {
    //设置user的值
    setUser(state, user) {
        state.user = user
    }
};
const actions = {
    //设置user的值
    setUser({commit}) {
        const url = USER_API;
        axios.get(url).then(response => {
            const data = response.data;
            if (data.code === -4) {
                commit("setUser", null);
                return
            }
            commit("setUser", data.data)
        }).catch(error => {
            console.log(error);
            this.$Message.error('网络异常，请稍后重试')
        })
    }
};
const getters = {};

export default new Vuex.Store({
    state,
    mutations,
    actions,
    getters,
    plugins: [createPersistedState()]
})
